/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mappers;

import handler.DatabaseConnection;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Commodity;
import model.CommodityToProduct;
import model.Product;

/**
 *
 * @author Benjamin
 */
public class CommodityToProductMapper {
    
    private CommodityToProductMapper() {
    }
    
    public static CommodityToProductMapper getInstance() {
        return CommodityToProductMapperHolder.INSTANCE;
    }
    
    private static class CommodityToProductMapperHolder {

        private static final CommodityToProductMapper INSTANCE = new CommodityToProductMapper();
    }
    
    public ArrayList<CommodityToProduct> getCommodities(Product product) throws SQLException, FileNotFoundException{
        ArrayList<CommodityToProduct> commoditiesToProduct = new ArrayList<>();
        
        Statement stmt = DatabaseConnection.getInstance().getConnection().createStatement();
        
        String sql = "select * from commoditytoproduct where productId = " + product.getId();
        
        ResultSet rs = stmt.executeQuery(sql);
        
        while(rs.next()){
            
            //Hent råvaren som produkt.
            int cId = rs.getInt("commodityId");
            Commodity commodity = CommodityMapper.getInstance().getCommodity(cId);
            
            int amount = rs.getInt("amount");
            
            commoditiesToProduct.add(new CommodityToProduct(commodity, product, amount));
        }
        
        return commoditiesToProduct;
    }
}
